package com.lbd.suggestion.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.suggestion.bean.Suggestion;
import com.lbd.suggestion.controller.vo.SuggestionVo;
import com.lbd.suggestion.dao.SuggestionRepository;
import com.lbd.suggestion.service.SuggestionService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by Yuanbo on 2016/9/26.
 * 投诉建议实现类
 */
@Service
@Transactional
public class SuggestionServiceImpl extends BaseServiceImpl<Suggestion, SuggestionRepository> implements SuggestionService {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    SuggestionRepository suggestionRepository;

    @Override
    public String buildJpql(Suggestion s, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder(" from Suggestion t");
        if (s instanceof SuggestionVo) {
            SuggestionVo suggestion = (SuggestionVo) s;
            if (StringUtils.isNotBlank(suggestion.getUserId())) {
                jpqlBuilder.append(" and t.userId like:userId");
                parameter.put("userId", "%" + suggestion.getUserId() + "%");
            }
            if (suggestion.getType() != null) {
                jpqlBuilder.append(" and t.type =:type");
                parameter.put("type", suggestion.getType());
            }
            if (suggestion.getStartDate() != null) {
                jpqlBuilder.append(" and t.createdDate >= :startDate");
                parameter.put("startDate", suggestion.getStartDate());
            }
            if (suggestion.getEndDate() != null) {
                jpqlBuilder.append(" and t.createdDate <=:endDate");
                parameter.put("endDate", suggestion.getEndDate());
            }
        }
        jpqlBuilder.append(" order by modifiedDate desc ");
        return jpqlBuilder.toString();
    }

}
